home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / CheckUSBPermissions.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  5.9 KB  |  170 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008, 2009, 2010 Red Hat, Inc.
  6. ## Authors:
  7. ##  Tim Waugh <twaugh@redhat.com>
  8.  
  9. ## This program is free software; you can redistribute it and/or modify
  10. ## it under the terms of the GNU General Public License as published by
  11. ## the Free Software Foundation; either version 2 of the License, or
  12. ## (at your option) any later version.
  13.  
  14. ## This program is distributed in the hope that it will be useful,
  15. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ## GNU General Public License for more details.
  18.  
  19. ## You should have received a copy of the GNU General Public License
  20. ## along with this program; if not, write to the Free Software
  21. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. import glob
  24. import os
  25. import subprocess
  26. from timedops import TimedSubprocess
  27. import urllib
  28. from base import *
  29. class CheckUSBPermissions(Question):
  30.     def __init__ (self, troubleshooter):
  31.         Question.__init__ (self, troubleshooter, "Check USB permissions")
  32.         troubleshooter.new_page (gtk.Label (), self)
  33.  
  34.     def display (self):
  35.         self.answers = {}
  36.         answers = self.troubleshooter.answers
  37.         if answers['cups_queue_listed']:
  38.             if answers['is_cups_class']:
  39.                 return False
  40.  
  41.             cups_printer_dict = answers['cups_printer_dict']
  42.             device_uri = cups_printer_dict['device-uri']
  43.         elif answers.get ('cups_device_listed', False):
  44.             device_uri = answers['cups_device_uri']
  45.         else:
  46.             return False
  47.  
  48.         (scheme, rest) = urllib.splittype (device_uri)
  49.         if scheme not in ['hp', 'hpfax', 'usb', 'hal']:
  50.             return False
  51.  
  52.         LSUSB = "/sbin/lsusb"
  53.         if not os.access (LSUSB, os.X_OK):
  54.             return False
  55.  
  56.         GETFACL = "/usr/bin/getfacl"
  57.         if not os.access (GETFACL, os.X_OK):
  58.             return False
  59.  
  60.         # Run lsusb
  61.         parent = self.troubleshooter.get_window ()
  62.         try:
  63.             self.op = TimedSubprocess (parent=parent,
  64.                                        args="LC_ALL=C " + LSUSB + " -v",
  65.                                        close_fds=True,
  66.                                        shell=True,
  67.                                        stdin=file("/dev/null"),
  68.                                        stdout=subprocess.PIPE,
  69.                                        stderr=subprocess.PIPE)
  70.             (lsusb_stdout, lsusb_stderr, result) = self.op.run ()
  71.         except:
  72.             # Problem executing command.
  73.             return False
  74.  
  75.         # Now parse it.
  76.         dev_by_id = {}
  77.         this_dev = None
  78.         for line in lsusb_stdout:
  79.             if (this_dev != None and
  80.                 ((line.find ("bInterfaceClass") != -1 and
  81.                   line.find ("7 Printer") != -1) or
  82.                  (line.find ("bInterfaceSubClass") != -1 and
  83.                   line.find ("1 Printer") != -1))):
  84.                 mfr = dev_by_id.get (this_mfr_id, {})
  85.                 mdl = mfr.get (this_mdl_id, [])
  86.                 mdl.append (this_dev)
  87.                 mfr[this_mdl_id] = mdl
  88.                 dev_by_id[this_mfr_id] = mfr
  89.                 this_dev = None
  90.                 continue
  91.  
  92.             separators = [ ('Bus ', 3),
  93.                            (' Device ', 3),
  94.                            (': ID ', 4),
  95.                            (':', 4),
  96.                            (' ', -1)]
  97.             fields = []
  98.             i = 0
  99.             p = line
  100.             while i < len (separators):
  101.                 (sep, length) = separators[i]
  102.                 if not p.startswith (sep):
  103.                     break
  104.                 start = len (sep)
  105.                 if length == -1:
  106.                     end = len (p)
  107.                     fields.append (p[start:])
  108.                 else:
  109.                     end = start + length
  110.                     fields.append (p[start:end])
  111.  
  112.                 p = p[end:]
  113.                 i += 1
  114.  
  115.             if i < len (separators):
  116.                 continue
  117.  
  118.             if not scheme.startswith ('hp') and fields[2] != '03f0':
  119.                 # Skip non-HP printers if we know we're using HPLIP.
  120.                 continue
  121.  
  122.             this_dev = { 'bus': fields[0],
  123.                          'dev': fields[1],
  124.                          'name': fields[4],
  125.                          'full': line }
  126.             this_mfr_id = fields[2]
  127.             this_mdl_id = fields[3]
  128.  
  129.         infos = {}
  130.         paths = []
  131.         if not scheme.startswith ('hp'):
  132.             paths.extend (glob.glob ("/dev/usb/lp?"))
  133.         for mfr_id, mdls in dev_by_id.iteritems ():
  134.             for mdl_id, devs in mdls.iteritems ():
  135.                 for dev in devs:
  136.                     path = "/dev/bus/usb/%s/%s" % (dev['bus'], dev['dev'])
  137.                     paths.append (path)
  138.                     infos[path] = dev['full']
  139.  
  140.         perms = []
  141.         for path in paths:
  142.             try:
  143.                 self.op = TimedSubprocess (parent=parent,
  144.                                            args="LC_ALL=C %s %s" % (GETFACL,
  145.                                                                     path),
  146.                                            close_fds=True,
  147.                                            shell=True,
  148.                                            stdin=file("/dev/null"),
  149.                                            stdout=subprocess.PIPE,
  150.                                            stderr=subprocess.PIPE)
  151.                 (getfacl_stdout, getfacl_stderr, result) = self.op.run ()
  152.                 output = filter (lambda x: len (x) > 0, getfacl_stdout)
  153.             except:
  154.                 # Problem executing command.
  155.                 output = []
  156.  
  157.             info = infos.get (path, path)
  158.             perms.append ((info, output))
  159.  
  160.         self.answers['getfacl_output'] = perms
  161.  
  162.         # Don't actually display anything, just collect information.
  163.         return False
  164.  
  165.     def collect_answer (self):
  166.         return self.answers
  167.  
  168.     def cancel_operation (self):
  169.         self.op.cancel ()
  170.